import cv2

img = cv2.imread('wangpingping.png', 0)
img = cv2.medianBlur(img,5)
ret,th1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
th2 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,2)
th3 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
ret2,th4 = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
blur = cv2.GaussianBlur(img,(5,5),0) # 阈值一定要设为 0！
ret3,th5 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

cv2.namedWindow("pic", cv2.WINDOW_FREERATIO)
cv2.namedWindow("th1", cv2.WINDOW_FREERATIO)
cv2.namedWindow("th2", cv2.WINDOW_FREERATIO)
cv2.namedWindow("th3", cv2.WINDOW_FREERATIO)
cv2.namedWindow("th4", cv2.WINDOW_FREERATIO)
cv2.namedWindow("th5", cv2.WINDOW_FREERATIO)
# cv2.resizeWindow("pic", 512, 512)
cv2.imshow("pic", img)
cv2.imshow("th1", th1)
cv2.imshow("th2", th2)
cv2.imshow("th3", th3)
cv2.imshow("th4", th4)
cv2.imshow("th5", th5)
if cv2.waitKey() & 0xFF == 27:
    cv2.destroyAllWindows()
